package com.example.payment2.controller;

import com.example.comment.config.Result;
import com.example.comment.entity.Payment;
import com.example.payment2.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author kuang
 * @Date 2021/7/11 18:09
 */
@RestController
@Slf4j
@RequestMapping(value = "/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/query/{id}")
    public Result<Payment> queryPayment(@PathVariable("id") Long id) {
        log.info("请求参数：{}", id);
        Result<Payment> result = new Result<>();
        Payment payment = paymentService.queryPayment(id);
        log.info("查询结果：{}", payment);
        if (payment != null) {
            result.setCode(200);
            result.setMessage("查询成功" + serverPort);
            result.setData(payment);
        } else {
            result.setCode(400);
            result.setMessage("没有" + id + "记录！");
            result.setData(null);
        }
        return result;
    }

    @PostMapping(value = "/add")
    public Result<Integer> addPayment(@RequestBody Payment payment) {
        int result = paymentService.addPayment(payment);
        log.info("新增支付：{}", payment);
        if (result > 0) {
            return new Result<>(200, "新增成功" + serverPort, result);
        } else {
            return new Result<>(400, "新增失败", null);
        }
    }

    @PostMapping(value = "/modify")
    public Result<Integer> modifyPayment(@RequestBody Payment payment) {
        log.info("数据修改：{}", payment);
        int result = paymentService.modifyPayment(payment);
        if (result > 0) {
            return new Result<>(200, "修改成功" + serverPort, result);
        } else {
            return new Result<>(400, "修改失败", null);
        }
    }

    @PostMapping(value = "/delete/{id}")
    public Result<Long> deletePayment(@PathVariable("id") Long id) {
        log.info(" 删除ID：{}", id);
        int result = paymentService.deletePayment(id);
        if (result > 0) {
            return new Result<>(200, "删除成功！" + serverPort, null);
        } else {
            return new Result<>(400, "删除失败！", id);
        }
    }

    @GetMapping(value = "/port")
    public String getServerPort(){
        return serverPort;
    }
}
